package tnviet.proj.jna.test;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author r_junqueira
 */
public class NewClass {

  public static void main(final String[] parArgs) {
    try {
      testSubstanceGradientError();
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }

  static public final void testSubstanceGradientError() throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {

      @Override
      public final void run() {
        try {
          final TestFrame locFrame = new TestFrame();
          locFrame.setVisible(true);
        } catch (Throwable e) {
          e.printStackTrace();
        }
      }
    });
  }

  static public final class TestFrame extends JXFrame {

    public TestFrame() {
      super("test");
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      setPreferredSize(new Dimension(400, 400));
      setLookAndFeel(this,
        "org.pushingpixels.substance.api.skin.SubstanceDustLookAndFeel", false);

      final JXTable locTestTable = new JXTable(new DummyTestTableModel());
      locTestTable.setColumnControlVisible(true);
      locTestTable.setSortable(true);

      getContentPane().add(new JScrollPane(locTestTable));
      pack();

      final JPopupMenu locPopupMenu = new JPopupMenu();
      locPopupMenu.addSeparator();
      locPopupMenu.add(new JMenuItem("test3"));
      locPopupMenu.addSeparator();
      locTestTable.addMouseListener(new TableMouseAdapter(locPopupMenu));
    }
  }

/// static
  static public final void setLookAndFeel(final Window parWindow, final String
parLookAndFeel, final boolean parUpdate) {
    try {
      UIManager.setLookAndFeel(parLookAndFeel);
      SwingUtilities.updateComponentTreeUI(parWindow);
      if (parUpdate) {
        parWindow.pack();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

/// class
  /// MouseAdapter for JTable popups
  private static final class TableMouseAdapter extends MouseAdapter {

    private final JPopupMenu _popupMenu;

    public TableMouseAdapter(final JPopupMenu parPopupMenu) {
      super();
      _popupMenu = parPopupMenu;
      assert (_popupMenu != null);
    }

    public final JPopupMenu getPopupMenu() {
      return _popupMenu;
    }

    @Override
    public final void mouseReleased(final MouseEvent parMouseEvent) {
      final Object locSource = parMouseEvent.getSource();
      assert (locSource instanceof JTable);
      final JTable locTable = (JTable) locSource;

      if (true == parMouseEvent.isControlDown()) {
        return;
      }
      if (true == parMouseEvent.isPopupTrigger() || true ==
SwingUtilities.isRightMouseButton(parMouseEvent)) {
        cancelCellEditing(locTable);
        _popupMenu.show(locTable, parMouseEvent.getX(), parMouseEvent.getY());
      }
    }

    private final void cancelCellEditing(final JTable parTable) {
      final TableCellEditor locCellEditor = parTable.getCellEditor();
      if (null != locCellEditor) {
        locCellEditor.cancelCellEditing();
      }
    }
  }

  /// fake TableModel
  private static final class DummyTestTableModel extends DefaultTableModel {

    @Override
    public final int getRowCount() {
      return 10;
    }

    @Override
    public final int getColumnCount() {
      return 10;
    }

    @Override
    public final String getColumnName(final int parColumnIndex) {
      return "C" + parColumnIndex;
    }

    @Override
    public final Class<?> getColumnClass(final int parColumnIndex) {
      return Double.class;
    }

    @Override
    public final boolean isCellEditable(final int parRowIndex, final int
parColumnIndex) {
      return false;
    }

    @Override
    public final Object getValueAt(final int parRowIndex, final int
parColumnIndex) {
      return (double) parColumnIndex;
    }
  }
}
